package com.lordjoe.collectiveintelligence.svm.matchmaker;

import com.lordjoe.collectiveintelligence.svm.*;
import libsvm.*;

import java.util.*;

/**
 * com.lordjoe.collectiveintelligence.svm.matchmaker.PairSVMWrapper
 *
 * @author Steve Lewis
 * @date May 25, 2009
 */
public class PairSVMWrapper extends AbstractSVMDataWrapper<PotentialPair>
{
    public static PairSVMWrapper[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = PairSVMWrapper.class;

    public PairSVMWrapper(PotentialPair in)
    {
        super(in);
    }

    public double getClassification()
    {
        PotentialPair data = getData();
        boolean b = data.isCompatable();
        if (b)
            return 1;
        else
            return -1;
    }

    public double getPredictedClassification()
     {
         PotentialPair data = getData();
         boolean b = data.isPredictedCompatable();
         if (b)
             return 1;
         else
             return -1;
     }

     public void setPredictedClassification(double pPredictedClassification)
     {
         PotentialPair data = getData();
         boolean b = data.isCompatable();
         if(pPredictedClassification == -1) {
              data.setPredictedCompatable(false);
         }
         else
             data.setPredictedCompatable(true);
         if(!data.isPredictionCorrect())
                b = data.isCompatable(); // break here
     }

    public static final double MAX_AGE = 70;
    public static final double MAX_DISTANCE = 10;

    public svm_node[] getAgeOnlyNodes()
      {
         List<svm_node> holder = new ArrayList<svm_node>();
         int index = 1;
          // 0 = male age
        index =  makeIndexedNode(holder, index, getData().getMaleAge() /MAX_AGE);
          // 1 = female age
        index =  makeIndexedNode(holder, index, getData().getFemaleAge() /MAX_AGE);

         svm_node[] ret = new svm_node[holder.size()];
         holder.toArray(ret);
         return ret;
      }
    public svm_node[] getNodes()
      {
       //   return getAgeOnlyNodes();
         // return getAgeAndDistance();
           return getAllNodes();
      }

    public svm_node[] getAgeAndDistance()
    {
       List<svm_node> holder = new ArrayList<svm_node>();
       int index = 1;
        // 0 = male age
      index =  makeIndexedNode(holder, index, getData().getMaleAge() /MAX_AGE);
        // 1 = female age
      index =  makeIndexedNode(holder, index, getData().getFemaleAge() /MAX_AGE);
        // 2 = male smoker
         // 6 = Distance
        // 2 = male smoker
      index =  makeIndexedNode(holder, index,
              SVMUtilities.booleanTo01(getData().isMaleSmoker()));
        // 3 = female smoker
      index =  makeIndexedNode(holder, index,
              SVMUtilities.booleanTo01(getData().isFemaleSmoker()));
        // 4 = male ChildrenWanted
        // 4 = male ChildrenWanted
      index =  makeIndexedNode(holder, index,
              SVMUtilities.booleanTo01(getData().isMaleChildrenWanted()));
        // 5 = female ChildrenWanted
      index =  makeIndexedNode(holder, index,
              SVMUtilities.booleanTo01(getData().isFemaleChildrenWanted()));
 
       svm_node[] ret = new svm_node[holder.size()];
       holder.toArray(ret);
       return ret;
    }


     public svm_node[] getAllNodes()
     {
        List<svm_node> holder = new ArrayList<svm_node>();
        int index = 1;
         // 1 = male age
         PotentialPair data = getData();
         index =  makeIndexedNode(holder, index, data.getMaleAge() /MAX_AGE);
         // 2 = female age
       index =  makeIndexedNode(holder, index, data.getFemaleAge() /MAX_AGE);
         // 3 = male smoker
       index =  makeIndexedNode(holder, index,
               SVMUtilities.booleanTo01(data.isMaleSmoker()));
         // 4 = female smoker
       index =  makeIndexedNode(holder, index,
               SVMUtilities.booleanTo01(data.isFemaleSmoker()));
         // 5 = male ChildrenWanted
       index =  makeIndexedNode(holder, index,
               SVMUtilities.booleanTo01(data.isMaleChildrenWanted()));
         // 6 = female ChildrenWanted
       index =  makeIndexedNode(holder, index,
               SVMUtilities.booleanTo01(data.isFemaleChildrenWanted()));
         // 7 = Distance
         double v = data.getDistance() / MAX_DISTANCE;
       //  v =  SVMUtilities.RND.nextGaussian();
         v = Math.min(v,1.0);
         v = Math.max(v,0.001);
        // System.out.println(v);
             index =  makeIndexedNode(holder, index,v);

        svm_node[] ret = new svm_node[holder.size()];
        holder.toArray(ret);
        return ret;
     }

     private int makeIndexedNode(List<svm_node> pHolder, int pIndex, double pX)
     {
         svm_node node = new svm_node();
         node.index = pIndex++;
         if(!Double.isNaN(pX) && !Double.isInfinite(pX))
            node.value = pX;
         pHolder.add(node);
         return pIndex;
     }

}
